#include "../include/head.h"
void Rtrim(char *string)
{
	size_t len = 0;
	if (string == NULL)
		return;

	len = strlen(string);
	while (len > 0 && string[len - 1] == ' ')   //位置换一下   
		string[--len] = 0;
	
	return;
}
void Ltrim(char *string)
{
	size_t len = 0;
	len = strlen(string);
	char *p_tmp = string;
	if ((*p_tmp) != ' ') //不是以空格开头
		return;
	//找第一个不为空格的
	while ((*p_tmp) != '\0')
	{
		if ((*p_tmp) == ' ')
			p_tmp++;
		else
			break;
	}
	if ((*p_tmp) == '\0') //全是空格
	{
		*string = '\0';
		return;
	}
	char *p_tmp2 = string;
	while ((*p_tmp) != '\0')
	{
		(*p_tmp2) = (*p_tmp);
		p_tmp++;
		p_tmp2++;
	}
	(*p_tmp2) = '\0';
	return;
}
pConf_t  load(const char *pconfName, pConf_t* conf)
{
	//pConf_t *pcur;
	int sum = 0;
	pconfNode_t pcur = (pconfNode_t)calloc(1, sizeof(confNode_t));
	FILE *fp;
	fp = fopen(pconfName, "r");
	if (fp == NULL)
		return NULL;
    else
    {
        printf("成功打开配置文件\n");
    }
	//每一行配置文件读出来都放这里
    
	char  linebuf[501];
	while (!feof(fp))
	{
		if (fgets(linebuf, 500, fp) == NULL)
			continue;
		if (linebuf[0] == 0)
			continue;
		//处理注释行
		if (*linebuf == ';' || *linebuf == ' ' || *linebuf == '#' || *linebuf == '\t' || *linebuf == '\n')
			continue;
	lblprocstring:
		//屁股后边若有换行，回车，空格等都截取掉
		if (strlen(linebuf) > 0)
		{
			if (linebuf[strlen(linebuf) - 1] == 10 || linebuf[strlen(linebuf) - 1] == 13 || linebuf[strlen(linebuf) - 1] == 32)
			{
				linebuf[strlen(linebuf) - 1] = 0;
				goto lblprocstring;
			}
		}
		if (linebuf[0] == 0)
			continue;
		if (*linebuf == '[') //[开头的也不处理
			continue;
		//==0肯定就是没读到数据
		
		
		char *ptmp = strchr(linebuf, '=');
		
		//走到=停下来
		//需要截取右边的
		if (ptmp != NULL)
		{
			
			strcpy((*conf)->ip,ptmp + 1);
			Rtrim((*conf)->ip);
			Ltrim((*conf)->ip);
		}
		pConf_t cf = (pConf_t)calloc(1, sizeof(Conf_t));
		strcpy(cf->ip, (*conf)->ip);
		
		if (pcur->size == 0)
		{
			pcur->phead = cf;
			pcur->ptail = cf;
			pcur->size++;
			
		}
		else
		{
			pcur->ptail->next = cf;
			pcur->ptail = cf;
			pcur->size++;
			
		}

	}
	pConf_t p;
	p = pcur->phead;
	while (pcur->phead)
	{
		printf("%s\n", pcur->phead->ip);
		pcur->phead = pcur->phead->next;
	}
	
	return p;

}